package com.dqp.cslggroup.JWXT;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dqp.cslggroup.UI.MyApplication;
import com.dqp.cslggroup.Util.SharedPreferencesUtil;
import com.dqp.cslggroup.bean.exam;

import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class GetStudentExam {
    private String url;
    private int year;
    private int term;
    private Map<String, String> exam_cookies;
    private Connection.Response response;
    private String stuNum;
    private String Authorization = SharedPreferencesUtil.getStringSp(MyApplication.getContext(), "Authorization", "");


    GetStudentExam(String exam_url, int year, int term, Map<String, String> cookies, String sno) {
        this.url = exam_url;
        this.year = year;
        this.term = term;
        this.exam_cookies = cookies;
        this.stuNum = sno;
    }

    List<exam> getExam() throws Exception {
        Map<String, String> datas = new HashMap<>();
        datas.put("xnm", String.valueOf(year));
        datas.put("xqm", String.valueOf(term * term * 3));
        datas.put("ksmcdmb_id", "");
        datas.put("kch", "");
        datas.put("kc", "");
        datas.put("ksrq", "");
        datas.put("_search", "false");
        datas.put("nd", String.valueOf(new Date().getTime()));
        datas.put("queryModel.showCount", "15");
        datas.put("queryModel.currentPage", "1");
        datas.put("queryModel.sortName", "");
        datas.put("queryModel.sortOrder", "asc");
        datas.put("time", "0");
       /* response = Jsoup.connect(url + "/jwglxt/kwgl/kscx_cxXsksxxIndex.html?gnmkdm=N358105&layout=default&su=" + stuNum)
                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36")
                .cookies(exam_cookies)
                .method(Connection.Method.POST)
                .data(datas)
                .ignoreContentType(true)
                .timeout(5000)
                .execute();*/
        response = Jsoup.connect(url + "/jwglxt/kwgl/kscx_cxXsksxxIndex.html?doType=query&gnmkdm=N358105&su=" + stuNum)
                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36")
                .cookies(exam_cookies)
                .header("Authorization", Authorization)
                .method(Connection.Method.POST)
                .data(datas)
                .ignoreContentType(true)
                .timeout(5000)
                .execute();
        if (response.body() != null && response.body().contains("{")) {
            JSONObject jsonObject = JSON.parseObject(response.body());
            List<exam> examList = new ArrayList<>();
            JSONArray gradeTable = JSON.parseArray(jsonObject.getString("items"));
            for (Object o : gradeTable) {
                JSONObject lesson = (JSONObject) o;
                exam exam = new exam();
                exam.setCdmc(lesson.getString("cdmc"));
                exam.setKcmc(lesson.getString("kcmc"));
                exam.setKsmc(lesson.getString("ksmc"));
                exam.setKssj(lesson.getString("kssj"));
                examList.add(exam);
            }
            return examList;
        }
        return null;
    }


}
